package thread.finalTest;

/**
 * Created by IntelliJ IDEA.
 * User: liuliwei
 * Date: 2017-09-29
 * Time: 下午2:55
 * Description: ${description}
 */
public class FinalTest {
    public Holder holder;

    public  synchronized void initialize() {
        holder = new Holder(3);
    }

    public static void main(String[] args) throws InterruptedException {
        while (true) {
            FinalTest finalTest = new FinalTest();
            Thread.yield();
//            Thread.sleep();
            new Thread(new SetRunnable(finalTest)).start();
            new Thread(new ShowRunnable(finalTest)).start();
        }
    }

    @Override
    public String toString() {
        return "FinalTest{" +
                "holder=" + holder +
                '}';
    }
}


class ShowRunnable implements Runnable {
    private FinalTest finalTest;

    public ShowRunnable(FinalTest finalTest) {
        this.finalTest = finalTest;
    }

    @Override
    public void run() {
        if (finalTest.holder==null) {
            System.out.println(finalTest);
        }
    }
}

class SetRunnable implements Runnable {
    private FinalTest finalTest;

    public SetRunnable(FinalTest finalTest) {
        this.finalTest = finalTest;
    }

    @Override
    public void run() {
        finalTest.initialize();
    }
}

class Holder {
    private final int value;

    public Holder(int value) {
        this.value = value;
    }

    public void assertSanity() {
        if (value!=value) {
            throw new AssertionError("this statement is false :");
        }
    }

    @Override
    public String toString() {
        return "Holder{" +
                "value=" + value +
                '}';
    }
}